package com.xing.photo;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: xingzhe
 * Date: 2015/3/23
 * Time: 17:11
 */
public class Plibao {
    public static void main(String[] args) throws Exception {
        HttpGet get = new HttpGet();
        for (int i = 11; i < 26; i++) {
            System.out.println("page: " + i);
            URI uri = new URI("http://91fuli.org/115/" + i + ".html");
            get.setURI(uri);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            CloseableHttpResponse response = httpClient.execute(get);
            HttpEntity re = response.getEntity();
            String content = EntityUtils.toString(re, "gbk");
            Document document = Jsoup.parse(content);
            Elements elements = document.select("div.trend.trendmsc>ul>li");
            for(Element element:elements){
                if(element.select("a[href]").first()!=null) {
                    String href = element.select("a[href]").first().attr("href");
                    String size="";
                    if(!href.contains("115网盘礼包码：")) {
                        size = getSize(href);
                    }else{
                        size = href.replace("115网盘礼包码：","");
                    }
                    if (!size.equals("")) {
                            String title = element.text().replace("福利", "");
//                System.out.println(title + "---" + href.substring(href.lastIndexOf("/")+1,href.length()) + "--" + size);
                            System.out.println("INSERT INTO code (Ftitle,Fsize,Fcode,Ftime) VALUES('" + title + "','" + size + "','" + href.substring(href.lastIndexOf("/") + 1, href.length()) + "'," + (int) (new Date().getTime() / 1000) + ");");
                        }
                }
            }
        }
    }

    public static String getSize(String href) throws IOException {
        URI uri = null;
        try {
            uri = new URI(href);
        } catch (URISyntaxException e) {
            return "";
        }
        HttpGet get = new HttpGet(uri);
                    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse response=null;
        try {
            response = httpClient.execute(get);
        }catch (IllegalStateException e){
            return "";
        }
        HttpEntity re = response.getEntity();
        String content = EntityUtils.toString(re, "utf-8");
        Document document = Jsoup.parse(content);
        if(document.select("div.spree-info>em")==null || document.select("div.spree-info>em").first()==null){
            return "";
        }
        String size = document.select("div.spree-info>em").first().text();
        size = size.replace("'大小：","");
        return size;
    }
}
